﻿using System;
using System.Collections.Generic;
using System.Linq;

using btmerc.API.Schemas.Interfaces;

namespace btmerc.API.Schemas
{
    public partial class GeneralContractTermAdjustments : IGeneralContractTermAdjustments
    {
        // TODO: we're not currently handling mercs or corps correctly;
        // we need to switch it to looking at types,
        // rather than string matches
        private Dictionary<ContractType, ContractTermAdjustments> _missionTerms;
        private Dictionary<Affiliation, ContractTermAdjustments> _employerTerms;
        private Dictionary<Rating, ContractTermAdjustments> _ratingTerms;

        public GeneralContractTermAdjustments(IEnumerable<DragoonsRatingTermAdjustments> ratingTerms,
                                              IEnumerable<EmployerTermAdjustments> employerTerms,
                                              IEnumerable<MissionTermAdjustments> missionTerms)
        {
            this.ratingAdjustmentsField = ratingTerms.ToList();
            this.employerAdjustmentsField = employerTerms.ToList();
            this.missionAdjustmentsField = missionTerms.ToList();
            _ratingTerms = ratingTerms.ToDictionary(r => r.Rating, r => (ContractTermAdjustments)r);
            _employerTerms = employerTerms.ToDictionary(e => e.Employer, e => (ContractTermAdjustments)e);
            _missionTerms = missionTerms.ToDictionary(m => m.Mission, m => (ContractTermAdjustments)m);
        }

        public float GetPaymentMultiplier(ContractType type, Employer employer, Rating dragoonsRating, bool isCovert)
        {
            float multiplier = (_missionTerms[type].PaymentMultiplier +
                                _employerTerms[employer.Affiliation].PaymentMultiplier +
                                _ratingTerms[dragoonsRating].PaymentMultiplier);
            if (isCovert)
            {
                multiplier += _missionTerms[ContractType.Covert].PaymentMultiplier;
            }
            return multiplier;
        }

        public byte GetDuration(ContractType type, Employer employer, Rating dragoonsRating)
        {
            return (byte) (_missionTerms[type].BaseLengthModifier *
                   (_employerTerms[employer.Affiliation].BaseLengthModifier + _ratingTerms[dragoonsRating].BaseLengthModifier));
        }

        public short GetCommandModifier(ContractType type, Employer employer, Rating dragoonsRating, bool isCovert)
        {
            short command = (short)(_missionTerms[type].CommandModifier +
                                    _employerTerms[employer.Affiliation].CommandModifier +
                                    _ratingTerms[dragoonsRating].CommandModifier);
            if (isCovert)
            {
                command += _missionTerms[ContractType.Covert].CommandModifier;
            }

            return command;
        }

        public short GetOverheadModifier(ContractType type, Employer employer, Rating dragoonsRating, bool isCovert)
        {
            short overhead = (short)(_missionTerms[type].OverheadModifier +
                                    _employerTerms[employer.Affiliation].OverheadModifier +
                                    _ratingTerms[dragoonsRating].OverheadModifier);
            if (isCovert)
            {
                overhead += _missionTerms[ContractType.Covert].OverheadModifier;
            }

            return overhead;
        }

        public short GetSalvageModifier(ContractType type, Employer employer, Rating dragoonsRating, bool isCovert)
        {
            short salvage = (short)(_missionTerms[type].SalvageModifier +
                                    _employerTerms[employer.Affiliation].SalvageModifier +
                                    _ratingTerms[dragoonsRating].SalvageModifier);
            if (isCovert)
            {
                salvage += _missionTerms[ContractType.Covert].SalvageModifier;
            }

            return salvage;
        }

        public short GetSupportModifier(ContractType type, Employer employer, Rating dragoonsRating, bool isCovert)
        {
            short support = (short)(_missionTerms[type].SupportModifier +
                                    _employerTerms[employer.Affiliation].SupportModifier +
                                    _ratingTerms[dragoonsRating].SupportModifier);
            if (isCovert)
            {
                support += _missionTerms[ContractType.Covert].SupportModifier;
            }

            return support;
        }

        public short GetTransportModifier(ContractType type, Employer employer, Rating dragoonsRating, bool isCovert)
        {
            short transport = (short)(_missionTerms[type].TransportModifier +
                                    _employerTerms[employer.Affiliation].TransportModifier +
                                    _ratingTerms[dragoonsRating].TransportModifier);
            if (isCovert)
            {
                transport += _missionTerms[ContractType.Covert].TransportModifier;
            }

            return transport;
        }
    }
}
